package com.fjd.controller;

import com.fjd.constant.JwtClaimsConstant;
import com.fjd.context.BaseContext;
import com.fjd.dto.CustomerDTO;
import com.fjd.dto.LoginDTO;
import com.fjd.dto.PCLoginDTO;
import com.fjd.entity.CustomerInfo;
import com.fjd.properties.JwtProperties;
import com.fjd.result.Result;
import com.fjd.service.CustomerService;
import com.fjd.utils.JwtUtil;
import com.fjd.vo.CustomerVO;
import com.fjd.vo.LoginVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.Console;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

@RestController("UserController")
@RequestMapping("/shopping/customer")
@Slf4j
@Api(tags = "用户登录")
public class CustomerController {

    @Autowired
    private CustomerService customerService;
    @Autowired
    private JwtProperties jwtProperties;

    /**
     * 微信登录
     *
     * @param loginDTO
     * @return
     */
    @PostMapping("/login")
    @ApiOperation("微信登录")
    public Result<LoginVO> login(@RequestBody LoginDTO loginDTO) {
        log.info("登录请求参数:{}", loginDTO);
        //微信登录
        CustomerInfo customerInfo = customerService.login(loginDTO);
        //为微信用户生产jwt令牌
        Map<String, Object> claims = new HashMap();
        claims.put(JwtClaimsConstant.CUSTOMER_ID, customerInfo.getCustomerId());
        String token = JwtUtil.createJWT(jwtProperties.getCustomerSecretKey(), jwtProperties.getCustomerTtl(), claims);
        //封装返回结果
        log.info("{}", customerInfo.getCustomerId());
        LoginVO loginVO = LoginVO.builder()
                .id(customerInfo.getCustomerId())
                .token(token)
                .openid(customerInfo.getOpenId())
                .build();
        return Result.success(loginVO);
    }

    @PostMapping("/pclogin")
    @ApiOperation("电脑端登录")
    public Result<LoginVO> pclogin(@RequestBody PCLoginDTO loginDTO) {
        log.info("登录请求参数: {}", loginDTO);
        CustomerInfo customerInfo;

        // 校验账号密码
        customerInfo = customerService.weblogin(loginDTO);

        // 为用户生成 JWT 令牌
        Map<String, Object> claims = new HashMap<>();
        claims.put(JwtClaimsConstant.CUSTOMER_ID, customerInfo.getCustomerId());
        String token = JwtUtil.createJWT(jwtProperties.getCustomerSecretKey(), jwtProperties.getCustomerTtl(), claims);

        // 封装返回结果
        LoginVO loginVO = LoginVO.builder()
                .token(token)
                .build();

        return Result.success(loginVO);
    }

    @GetMapping("/info")
    @ApiOperation("获取用户信息")
    public Result<CustomerVO> getInfo() {
        CustomerVO customerVO = customerService.getInfo();
        return Result.success(customerVO);
    }

    @PostMapping("/info/edit")
    @ApiOperation("修改用户信息")
    public Result edit(@RequestBody CustomerDTO customerDTO) {
        customerService.editById(customerDTO);
        return Result.success();
    }

    @GetMapping("/info/all")
    @ApiOperation("获取用户信息")
    public Result<CustomerInfo> getAllInfo() {
        CustomerInfo customerInfo = customerService.getAllInfo();
        return Result.success(customerInfo);
    }
}